package orbitPrototype.objects;

import java.awt.Rectangle;

import orbitPrototype.Util;
import orbitPrototype.loaders.XMLLoader;

/**
 * Description: Contains sprite. Width and height is determined by the sprite (looked up in xml).
 * 				As a sprite shouldn't be larger or smaller than what is intended, there's no point
 * 				in having instantiation of the class determining size.
 *
 * 				_spriteType:		The type of sprite/what file it is in.
 * 				_spriteUniqueName:	The unique name of the sprite within the xml lookup file related to the file.	
 * 				_sourceRectangle:	The part of the spritesheet that is this sprite.
 * 
 *  
 * @author Drapan
 *
 */
public class Sprite extends DisplayObject
{
	private String _spriteType = "";
	private String _spriteUniqueName = "";
	private Rectangle _sourceRectangle = null;
	
	private double _rotation = 0;
	//private boolean _stretch = true;
	
	/**
	 * Description:	Creates a new sprite, from the specified spritesheet (spriteType) and from the uniquename 
	 * 				(spriteUniqueName) specified.
	 * 
	 * @param x
	 * @param y
	 * @param spriteType
	 * @param spriteUniqueName
	 */
	public Sprite(int x, int y, String spriteType, String spriteUniqueName)
	{
		super(x, y, 
				Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
				DisplayObject.TYPE_SPRITE);
		
		_spriteType = spriteType;
		_spriteUniqueName = spriteUniqueName;

		_sourceRectangle = new Rectangle(Integer.parseInt(XMLLoader.getElementValue("x", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
				Integer.parseInt(XMLLoader.getElementValue("y", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))));
		
	}
	
	/**
	 * Description:	Creates a new sprite, from the specified spritesheet (spriteType) and from the uniquename 
	 * 				(spriteUniqueName) specified.
	 * 
	 * @param x
	 * @param y
	 * @param rotation
	 * @param spriteType
	 * @param spriteUniqueName
	 */
	public Sprite(int x, int y, double rotation, String spriteType, String spriteUniqueName)
	{
		super(x, y, 
				Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
				DisplayObject.TYPE_SPRITE);
		
		_rotation = rotation;
		_spriteType = spriteType;
		_spriteUniqueName = spriteUniqueName;

		_sourceRectangle = new Rectangle(Integer.parseInt(XMLLoader.getElementValue("x", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
				Integer.parseInt(XMLLoader.getElementValue("y", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))));
		
		
	}
	
	/**
	 * Description:	Creates a new sprite, from the specified spritesheet (spriteType) and from the uniquename 
	 * 				(spriteUniqueName) specified. NOTE: This constructor includes width and height, which forces
	 * 				the sprite to this size, by stretching it.
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param spriteType
	 * @param spriteUniqueName
	 */
	public Sprite(int x, int y, int width, int height, String spriteType, String spriteUniqueName)
	{
		super(x, y, width, height, DisplayObject.TYPE_SPRITE);
		
		_spriteType = spriteType;
		_spriteUniqueName = spriteUniqueName;

		_sourceRectangle = new Rectangle(Integer.parseInt(XMLLoader.getElementValue("x", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
				Integer.parseInt(XMLLoader.getElementValue("y", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))));
		
		
	}
	
	/**
	 * Description:	Creates a new sprite, from the specified spritesheet (spriteType) and from the uniquename 
	 * 				(spriteUniqueName) specified. NOTE: This constructor includes width and height, which forces
	 * 				the sprite to this size, by cropping it - specified by the boolean "stretch".
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param stretch
	 * @param spriteType
	 * @param spriteUniqueName
	 */
	public Sprite(int x, int y, int width, int height, boolean stretch, String spriteType, String spriteUniqueName)
	{
		super(x, y, width, height, DisplayObject.TYPE_SPRITE);
		
		_spriteType = spriteType;
		_spriteUniqueName = spriteUniqueName;

		if(stretch)
		{
			_sourceRectangle = new Rectangle(Integer.parseInt(XMLLoader.getElementValue("x", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
					Integer.parseInt(XMLLoader.getElementValue("y", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
					Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
					Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))));
		}
		else
		{
			if(width > Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName)))
					&& height > Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))))
			{
				Util.error("Shouldn't crop Sprite to a size larger than specifed size in spriteLookup for spriteType: " + spriteType
						+ ", spriteUniqueName: " + spriteUniqueName + " for size: " + width + " x " + height);
			}
			
			_sourceRectangle = new Rectangle(Integer.parseInt(XMLLoader.getElementValue("x", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
					Integer.parseInt(XMLLoader.getElementValue("y", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
					width, height);
		}
		
	}
	
	/**
	 * Description:	Creates a new sprite, from the specified spritesheet (spriteType) and from the uniquename 
	 * 				(spriteUniqueName) specified. NOTE: This constructor includes width and height, which forces
	 * 				the sprite to this size, by stretching it.
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param rotation
	 * @param spriteType
	 * @param spriteUniqueName
	 */
	public Sprite(int x, int y, int width, int height, double rotation, String spriteType, String spriteUniqueName)
	{
		super(x, y, width, height, DisplayObject.TYPE_SPRITE);
		
		_rotation = rotation;
		_spriteType = spriteType;
		_spriteUniqueName = spriteUniqueName;

		_sourceRectangle = new Rectangle(Integer.parseInt(XMLLoader.getElementValue("x", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
				Integer.parseInt(XMLLoader.getElementValue("y", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
				Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))));
		
		
	}
	

	/**
	 * Description:	Creates a new sprite, from the specified spritesheet (spriteType) and from the uniquename 
	 * 				(spriteUniqueName) specified. NOTE: This constructor includes width and height, which forces
	 * 				the sprite to this size, by cropping it - specified by the boolean "stretch".
	 * 
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param rotation
	 * @param stretch
	 * @param spriteType
	 * @param spriteUniqueName
	 */
	public Sprite(int x, int y, int width, int height, double rotation, boolean stretch, String spriteType, String spriteUniqueName)
	{
		super(x, y, width, height, DisplayObject.TYPE_SPRITE);
		
		_rotation = rotation;
		_spriteType = spriteType;
		_spriteUniqueName = spriteUniqueName;

		if(stretch)
		{
			_sourceRectangle = new Rectangle(Integer.parseInt(XMLLoader.getElementValue("x", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
					Integer.parseInt(XMLLoader.getElementValue("y", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
					Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
					Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))));
		}
		else
		{
			if(width > Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName)))
					&& height > Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))))
			{
				Util.error("Shouldn't crop Sprite to a size larger than specifed size in spriteLookup for spriteType: " + spriteType
						+ ", spriteUniqueName: " + spriteUniqueName + " for size: " + width + " x " + height);
			}
			
			_sourceRectangle = new Rectangle(Integer.parseInt(XMLLoader.getElementValue("x", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))), 
					Integer.parseInt(XMLLoader.getElementValue("y", XMLLoader.getXMLElementByUniqueName("spriteLookup_" + spriteType, spriteUniqueName))),
					width, height);
		}
		
	}

	public void setRotation(int newValue)
	{
		_rotation = newValue;
	}
	
	public double getRotation()
	{
		return _rotation;
	}
	
	public String getSpriteType()
	{
		return _spriteType;
	}

	public String getSpriteUniqueName()
	{
		return _spriteUniqueName;
	}
	
	public Rectangle getSourceRectangle()
	{
		return _sourceRectangle;
	}
}
